package com.yuzhenxin.coursesystem.listener;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.yuzhenxin.coursesystem.entity.TeacherDO;
import com.yuzhenxin.coursesystem.service.TeacherService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
public class TeacherListener extends AnalysisEventListener<TeacherDO> {

    private final TeacherService teacherService;

    private final List<TeacherDO> teacherEntityList = new ArrayList<>();

    @Override
    public void invoke(TeacherDO TeacherEntity, AnalysisContext analysisContext) {
        teacherEntityList.add(TeacherEntity);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (ObjectUtil.isNotEmpty(teacherEntityList)) {
            teacherService.saveBatch(teacherEntityList);
            log.info("保存数据成功");
        }
    }
}